<template>
  <div class="assign-role">
    <!-- 角色列表 -->
    <div>
      <el-checkbox-group v-model="checkList">
        <el-checkbox v-for="item in list " :key="item.id" :label="item.id">
          {{ item.name }}
        </el-checkbox>
        <!-- <el-checkbox label="复选框 B" />
        <el-checkbox label="复选框 C" />
        <el-checkbox label="禁用" disabled />
        <el-checkbox label="选中且禁用" disabled /> -->
      </el-checkbox-group>
    </div>
    <!-- 确认按钮 -->
    <div class="baten">
      <el-button type="primary" round @click="onSubmit">确认</el-button>
      <el-button round @click="concen">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoleLIST } from '@/api/settings'
import { userInfoAssignlRequest, userInfoDateilRequest } from '@/api/user'
export default {
  name: '',
  props: {
    currentId: {
      type: String,
      default: ''

    }
  },
  data() {
    return {
      checkList: [],
      list: []
    }
  },
  created() {
    this.getsettingslist()
  },
  methods: {
    concen() {
      this.$emit('close')
    },
    // 获取所有角色列表的axios步骤 { page: 1, pagesize: 10000 }就是为了节约data里面的数据且就是避免在上面定义一个form
    async getsettingslist() {
      try {
        const res = await getRoleLIST({ page: 1, pagesize: 10000 })
        console.log('获取所有角色列表', res)

        this.list = res.data?.rows
      } catch (error) {
        console.log('获取信息失败')
      }
      // 根据id获取用户信息接口
      const res2 = await userInfoDateilRequest(this.currentId)
      console.log(res2, 666666666666)
      if (res2.success) {
        this.checkList = res2.data.roleIds || []
      } else {
        this.$message.error(res2.message || '获取用户信息接口失败')
      }
    },
    // 确认按钮
    async  onSubmit() {
      try {
        const res = await userInfoAssignlRequest({
          id: this.currentId,
          roleIds: this.checkList

        })
        if (res.success) {
          this.concen()
        } else {
          this.$message.error(res.message)
        }
        console.log('userInfoAssignlRequest: ', res)
      } catch (err) {
        console.log('userInfoAssignlRequest: ', err)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.assign-role{
    .baten{
text-align: right;
    }
}
</style>
